﻿using NewsPortal.Domain.PostEntities;
using System.Data.Entity.ModelConfiguration;

namespace NewsPortal.DataAccess.POCO.Config
{
    public class PostConfig: EntityTypeConfiguration<Post>
    {
        public PostConfig()
        {
            this.HasKey(p => p.Id);
            this.Property(p => p.Title).IsRequired();
            this.Property(p => p.Description).IsRequired();
            this.Property(p => p.Body).IsRequired();

            this.HasRequired(p => p.Categories);

            this.HasMany(p => p.Comments)
                .WithRequired(c => c.Post)
                .HasForeignKey(c => c.PostId);

            this.HasMany(p => p.Categories).WithMany(pc => pc.Posts)
                .Map(p => p.MapLeftKey("PostId").MapRightKey("CategoryId").ToTable("PostPostCategory"));

            this.HasMany(p => p.Tags).WithMany(t => t.Posts)
                .Map(p => p.MapLeftKey("PostId").MapRightKey("TagId").ToTable("PostTag"));
        }
    }
}
